﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' 
	AND name = 'JmdSiteSave')
BEGIN
		DROP  Procedure  dbo.JmdSiteSave
END
GO
/*
	Copyright (C) 2008  Ajay Handa - jmdcms.com 
	This program is free software; 
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	License for more details.
	Removing/Altering this copyright message is a violation of license terms.
*/

/*
	*** D O C U M E N T A T I O N ***
	---------------------------------
	Author: Ajay Handa
	Last Changed On: January-23-2008
	Purpose: Insert/Update into JmdSite table
*/


CREATE Procedure dbo.JmdSiteSave
	@ID uniqueidentifier,
	@ParentSiteID uniqueidentifier,
	@AliasSiteID uniqueidentifier,
	@SiteURL nvarchar(100) ,
	@SiteFolder nvarchar(100),
	@LastChanged timestamp,	
	@NewLastChanged timestamp output
AS
  if exists ( Select * From JmdSite where id = @ID)
  begin
	Update
		JmdSite
	Set
		ParentSiteID = @ParentSiteID,
		AliasSiteID = @AliasSiteID,
		SiteURL = @SiteURL,
		SiteFolder = @SiteFolder
	Where
		Id = @Id And
		LastChanged = @LastChanged
	If @@ROWCOUNT = 0
	begin
	    RAISERROR('Row has been edited by another user', 16, 1)  
	    return
	end    
  end
  else
  begin
	Insert into JmdSite
	(
		ParentSiteId, AliasSiteId, SiteURL, SiteFolder
	)
	values
	(
		@ParentSiteId, @AliasSiteID, @SiteURL, @SiteFolder
	)
  end
  
	Select
		@NewLastChanged = LastChanged
  	From
  		JmdSite
  	Where
  		Id = @Id		

GO


